package cn.edu.scau.dormitory_management_system.service.impl;

import cn.edu.scau.dormitory_management_system.dao.RoleDao;
import cn.edu.scau.dormitory_management_system.entity.RoleEntity;
import cn.edu.scau.dormitory_management_system.entity.UserEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.RoleDto;
import cn.edu.scau.dormitory_management_system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author whj
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {
    @Autowired(required = false)
    private RoleDao roleDao;
    @Override
    public int insertRole(String roleName, String remark, UserEntity user){
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRoleName(roleName);
        roleEntity.setRemark(remark);
        roleEntity.setCreateBy(String.valueOf(user.getUserId()));
        int insert = roleDao.insert(roleEntity);
        if(insert > 0){
            return roleEntity.getRoleId();
        }
        return insert;
    }

    @Override
    public int updateRole(int roleId, String roleName, String remark, UserEntity user) {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRoleId(roleId);
        roleEntity.setRoleName(roleName);
        roleEntity.setRemark(remark);
        roleEntity.setLastUpdateBy(String.valueOf(user.getUserId()));
        int update = roleDao.updateById(roleEntity);
        return update;
    }

    @Override
    public List<RoleEntity> searchRole(String search) {
        char[] chars = search.toCharArray();
        return roleDao.searchRole(chars);
    }
}